package com.mm.gateway.filters;

import cn.hutool.core.text.AntPathMatcher;
import com.mm.common.config.RedisProperties;
import com.mm.gateway.config.AuthProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @BelongsProject: 6.hm-shop
 * @BelongsPackage: com.hmall.gateway.filters
 * @Author : 游在海里的鱼
 * @CreateTime : 2024-05-07  16:30
 * @Version : 1.0
 * @ClassName : TODO
 * @Description : 登录拦截
 */
@Component
@RequiredArgsConstructor
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    //基于 Ant 风格的路径匹配
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
    private final StringRedisTemplate stringRedisTemplate;
    private final AuthProperties authProperties;
    private final RedisProperties redisProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取request
        ServerHttpRequest request = exchange.getRequest();

        //判断是否需要进行登录拦截
        if(isExclude(request.getPath().toString())){
            return chain.filter(exchange);
        }

        //获取token
        List<String> headers = request.getHeaders().get("session-token");
        if(headers == null || headers.get(0)==null){
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.FOUND);
            response.getHeaders().set(HttpHeaders.LOCATION, "http://159.75.118.180:58081/sign-in.html");
            return response.setComplete();
        }

        String token = headers.get(0);
        String key = redisProperties.session_user_pre + token;
        if(!stringRedisTemplate.hasKey(key)){
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.FOUND);
            response.getHeaders().set(HttpHeaders.LOCATION, "http://159.75.118.180:58081/sign-in.html");
            return response.setComplete();
        }


        //保存用户信息到请求头
        ServerWebExchange swe = exchange.mutate()
                .request(b -> b.header("session-token",token))
                .build();
        //放行
        return chain.filter(swe);
    }

    /* ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 判断路径是否需要登录检验
    **/
    private boolean isExclude(String path) {
        for(String pathPattern: authProperties.getExcludePaths()){
            if(antPathMatcher.match(pathPattern,path)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
